package com.ccpp.springcloud.controller;

import com.alibaba.fastjson.JSON;
import com.ccpp.demo.entity.User;
import com.ccpp.springcloud.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;

/**
 * User Feign Controller
 *
 * @author Liuguanglei liugl@ekeyfund.com
 * @create 2017-06-下午1:50
 */
@RestController
public class UserFeignController {

    @Autowired
    UserService userService;
    @Autowired
    RestTemplate restTemplate;

    @GetMapping(value = "/list")
    public List<User> list(){
        return userService.list();
    }

    @GetMapping("/login")
    public User login(@RequestParam String name,@RequestParam String password){
        return userService.login(name,password);
    }

    @RequestMapping("/testSession")
    public String getSession() {
        return  userService.testSession();
    }

    @RequestMapping("getUser")
    public List<User> listUser() {
        User users = this.restTemplate.getForObject("http://demo/test/getUser", User.class);
        List<User> userList = Arrays.asList(users);

        ResponseEntity<User> user = restTemplate.getForEntity("http://demo/test/getUser", User.class);
        System.out.println(JSON.toJSONString(user.getBody()));
        return userList;
    }

}
